﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Sales
{
    public partial class frmMaintainIndustryType : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public MstIndustryTypeEntity SelectIndustryType { get; set; }
        #endregion

        #region Constructor
        public frmMaintainIndustryType()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetFields()
        {
            if (SelectIndustryType == null)
            {
                return;
            }
            txtIndustryTypeCode.Text = SelectIndustryType.IndustryTypeCode;
            txtIndustryTypeName.Text = SelectIndustryType.IndustryTypeName;
            txtIndustryTypeRemark.Text = SelectIndustryType.Description;
        }

        private void SetEntity()
        {
            if (SelectIndustryType == null)
            {
                SelectIndustryType = new MstIndustryTypeEntity();
            }
            SelectIndustryType.IndustryTypeCode = txtIndustryTypeCode.Text;
            SelectIndustryType.IndustryTypeName = txtIndustryTypeName.Text;
            SelectIndustryType.Description = txtIndustryTypeRemark.Text;
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtIndustryTypeCode.Text))
            {
                MessageBox.Show("行业编码不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtIndustryTypeName.Text))
            {
                MessageBox.Show("行业名称不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool IsExist = MstIndustryTypeBs.CheckCodeIsExist(txtIndustryTypeCode.Text);
                    if (IsExist)
                    {
                        MessageBox.Show("该编码已经存在了！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    bool isSuccessful = MstIndustryTypeBs.InsertMstIndustryType(SelectIndustryType);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = MstIndustryTypeBs.UpdateMstIndustryType(SelectIndustryType);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            if (ThisActionType == ActionType.New)
            {
                this.Text = "行业类型维护--新增";
            }
            else if (ThisActionType == ActionType.Modify)
            {
                txtIndustryTypeCode.Properties.ReadOnly = true;
                this.Text = "行业类型维护--修改";
            }
            SetFields();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        #endregion
    }
}
